package de.gameduell.framework.facebook.testing {
	import de.gameduell.framework.asset.AssetGroup;

	import trace;
	import de.gameduell.framework.asset.Asset;
	import de.gameduell.framework.asset.AssetEvent;
	import de.gameduell.framework.asset.BinaryAsset;
	import de.gameduell.framework.asset.XMLAsset;
	import de.gameduell.framework.debug.Debug;
	import de.gameduell.framework.queue.AssetAction;
	import de.gameduell.framework.queue.GDAction;
	import de.gameduell.framework.util.FlashVars;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFormat;

	public class TestMode {

		private var _testmodes:Array;
		private var _enabled:Boolean; 
		private var _baseURL:String;
		private var _inGameBaseURL:String;
		private var _appAssets:Array;
		private var _inGameAssets:Array;
		private var _logOutput:TextField;
		private var _sprite:Sprite;
		
		private var _testOK:Boolean;
		private var _ingameXMLFolder:String;

		public function TestMode(aBaseURL:String, appSprite:Sprite){
			Debug.trace("TestMode::TestMode");
			_enabled = FlashVars.getKeyValue(FlashVars.TESTMODE) == "true";
			_testmodes = new Array();
			_baseURL = aBaseURL;
			
			_inGameBaseURL = _baseURL.substr(0, _baseURL.lastIndexOf("/"));
			_inGameBaseURL = _inGameBaseURL.substr(0, _inGameBaseURL.lastIndexOf("/") + 1);
			
			_logOutput = new TextField();
			_sprite = appSprite;
			_logOutput.width = 760;
			_logOutput.height = 600;
			_sprite.addChild(_logOutput);
			
			var tf:TextFormat = new TextFormat();
			tf.font = "_sans";
			_logOutput.background = true;
			_logOutput.textColor = 0x666666;
			_logOutput.defaultTextFormat = tf;
			log("TestMode active\n\n");
		}
		
		public function testAppAssetLoading(assetList:Array):void {
			Debug.trace("TestMode::testAppAssetLoading");
			_testOK = true;
			_appAssets = assetList;
			log("testing application assets\n-------------------------------------\n");
			for(var assetId in assetList){
				loadAsset(assetId);
			}
			new GDAction(testDone, ["application assets"], true, "doneAppAssetLoading");
		}
		
		private function testDone(type:String):void {
			var success:String = _testOK ? "OK" : "FAILED";
			log("-------------------------------------\n" + type + "  " + success + "\n\n");
		}

		public function testInGameAssetLoading(xmlFolder:String):void {
			_testOK = true;
			_ingameXMLFolder = xmlFolder;
			log("testing ingame assets\n-------------------------------------\n");
			
			var configAsset:XMLAsset = new XMLAsset("config", _inGameBaseURL + xmlFolder + "/" + "config.xml");
			configAsset.addEventListener(AssetEvent.COMPLETE, onLoadingComplete);
			configAsset.addEventListener(AssetEvent.COMPLETE, inGameConfigLoaded);
			configAsset.addEventListener(AssetEvent.LOAD_ERROR, onLoadError);
			configAsset.load();
		}
		
		private function inGameConfigLoaded(event:AssetEvent):void {
			log("parsing ingame config\n");
			var config:XML = XML(event.asset.data);
			var assetsList:XMLList = config.child("assets");
			var assets:AssetGroup  = new AssetGroup("assets", _inGameBaseURL + _ingameXMLFolder);
			// TODO - load ingame assets
		}

		public function loadAsset(anAssetId:String):void {
			var loader:Asset = new BinaryAsset(anAssetId, _baseURL + _appAssets[anAssetId]);
			loader.addEventListener(AssetEvent.COMPLETE, onLoadingComplete);
			loader.addEventListener(AssetEvent.LOAD_ERROR, onLoadError);
			new AssetAction(loader, true, anAssetId);
		}
		
		private function onLoadingComplete(event:AssetEvent):void {
			log("DONE loading: ID=" + event.asset.id + " URL=" + event.asset.url + "\n");
		}
		
		private function onLoadError(event:AssetEvent):void {
			_testOK = false;
			log("FAILED to load: ID=" + event.asset.id + " URL=" + event.asset.url + "\n");
		}
		
		private function log(aLogMessage:String):void {
			_logOutput.appendText(aLogMessage);
		}
	}
}
